<?php

namespace app\admin\controller\distribution;

use app\common\model as M;
use app\admin\controller\AuthController;
use app\admin\model\system\SystemConfig as ConfigModel;
use app\common\model\DistributionConfig;
use service\JsonService;
use service\UploadService;
use think\Log;

/**
 *  分销配置控制器
 * Class SystemConfig
 * @package app\admin\controller\system
 */
class Setting extends AuthController
{
    /**
     * 基础配置
     * */
    public function index()
    {
        return $this->fetch();
    }

    public function getData()
    {
        $user = $this->adminInfo;
        $result = DistributionConfig::where('org_id', $user['org_id'])->select();
        return JsonService::successful("success", $result);
    }

    public function updateData()
    {
        $user = $this->adminInfo;
        $res = DistributionConfig::where("id", ">", 0)->where('org_id', $user['org_id'])->delete();
        foreach (input() as $k => $v) {
            $config = new DistributionConfig();
            $config->key = $k;
            $config->value = $v;
            $config->save();
        }
        return JsonService::successful("更新成功");
    }

    public function view_upload()
    {
        $res = UploadService::Image('file', 'config/image');
        if (!$res->status) return JsonService::fail($res->error);
        return JsonService::successful('上传成功!', ['url' => $res->filePath]);
    }

    /**
     * 默认佣金比例
     * @Author   FW
     * @DateTime 2021-09-08
     * @Email    123763712@qq.com
     * @return   [type]           [description]
     */
    public function default_commisson ()
    {
        return $this->fetch();
    }


    public function save_distribution_seperate ()
    {
        $post = $this->request->post();

        if ( !isset($post['default_commission']) || !is_array($post['default_commission']) ) {
            return JsonService::fail('缺少设置项');
        }

        if ( !isset($post['relation_type']) || $post['relation_type'] == 0 ) {
            return JsonService::fail('缺少参数：relation_type');
        }

        if ( !isset($post['relation_id']) || $post['relation_id'] == 0 ) {
            return JsonService::fail('缺少参数：relation_id');
        }

        $res = M\DistributionSeparate::save_commission($post);
        if ( $res !== false ) return JsonService::successful('保存成功');

        return JsonService::fail('操作失败');
    }

    public function get_distribution_seperate ($relation_type , $relation_id)
    {
        return JsonService::successful('successful' , M\DistributionSeparate::get_commission($relation_type , $relation_id));
    }
	
	public function default_commission ()
	{
		$config = M\DistributionConfig::where('key','default_commission')->value('value');
		if ( !empty($config) ) {
			$config = json_decode($config);
		}
		return JsonService::successful($config);
	}
	
	public function save_default_commission ()
	{
	    $post = $this->request->post();

	    if ( !isset($post['default_commission']) || !is_array($post['default_commission']) ) {
	        return JsonService::fail('缺少设置项');
	    }
	
	    $res = M\DistributionConfig::saveOne([
			'key'   => 'default_commission',
			'value' => json_encode($post['default_commission']) 
		]);
	    if ( $res !== false ) return JsonService::successful('保存成功');
	
	    return JsonService::fail('操作失败');
	}
} //Class End
